package com.weixin.base.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.weixin.base.dao.SiteDao;
import com.weixin.base.util.Tools;
import com.weixin.common.BaseSpringJdbcDao;
import com.weixin.entity.TbWxCustomMenu;
import com.weixin.entity.TbWxMenu;
import com.weixin.entity.TbWxMessage;
import com.weixin.entity.TbWxText;
import com.weixin.entity.TbWxUser;
import com.weixin.entity.TbWxUserUrl;

public class SiteDaoImpl extends BaseSpringJdbcDao<TbWxMenu, Integer> implements SiteDao {

	public Class getEntityClass() {
		return TbWxMenu.class;
	}
	
	/**
	 * 首页模版查询
	 */
	public int selectSiteTemplate(int nUserId){
		int result=0;
		try {
			String sql="SELECT top 1 nTemplateId from tbwxuser where nUserId="+nUserId;
			result=getSimpleJdbcTemplate().queryForInt(sql);
		} catch (Exception e) {
			result=0;
		}
		
		return result;
	}
	/**
	 * 修改微信模版
	 */
	public int updateSiteTemplate(int nUserId,int nTemplateId) {
		String sql = "update tbwxuser set nTemplateId="+nTemplateId+" WHERE nUserId="+nUserId;
		int result = getSimpleJdbcTemplate().update(sql);
		return result > 0 ? 1 : 2;
	}
	
	/**
	 * 查询appId, appSecret
	 */
	public Map<String, Object> selectApp(int nuserId){
		String sql = "SELECT sAppID,sAppSecret FROM tbwxuser where nUserId=" + nuserId + " and nIsValid=1 ";
		return getSimpleJdbcTemplate().queryForMap(sql);
	}
	
	/**
	 * 删除自定义菜单 result 返回值：1.成功 2.失败
	 */
	public int delCustomMenu(int nWxCustomMenuId){
		String sql = " DELETE FROM tbwxcustommenu where nWxCustomMenuId=" + nWxCustomMenuId+" or npid="+nWxCustomMenuId;
		int result = getSimpleJdbcTemplate().update(sql);
		return result > 0 ? 1 : 2;
	}

	/**
	 * 修改自定义菜单 result 返回值：1.成功 2.失败
	 */
	public int updateCustomMenu(TbWxCustomMenu tbWxCustomMenu) {
		// 获取链接地址
		tbWxCustomMenu.setSWxUserUrl(selectWxUserUrl(tbWxCustomMenu.getNWxUserUrlId()));

		String sql = " UPDATE tbwxcustommenu set ";
		sql += " sName = :sName,ntype = :ntype,nKey=:nKey,nWxUserUrlId = :nWxUserUrlId,sWxUserUrl = :sWxUserUrl,nOrder=:nOrder,sRemark=:sRemark ";
		sql += " WHERE nUserId=:nUserId and nWxCustomMenuId=:nWxCustomMenuId ";
		int result = getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(tbWxCustomMenu));

		return result > 0 ? 1 : 2;
	}

	/**
	 * 添加自定义菜单 result 返回值：1.成功 2.失败
	 */
	public int saveCustomMenu(TbWxCustomMenu tbWxCustomMenu) {
		// 获取链接地址
		tbWxCustomMenu.setSWxUserUrl(selectWxUserUrl(tbWxCustomMenu.getNWxUserUrlId()));

		String sql = "INSERT INTO tbwxcustommenu (nPid,sName,ntype,nKey,nWxUserUrlId,sWxUserUrl,nUserId,nOrder,sRemark,dCreateTime) "
				+ "VALUES (:nPid,:sName,:ntype,:nKey,:nWxUserUrlId,:sWxUserUrl,:nUserId,:nOrder,:sRemark,:dCreateTime) ";
		int result = getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(tbWxCustomMenu));

		return result > 0 ? 1 : 2;
	}

	/**
	 * 查询自定义菜单
	 */
	public List<Map<String, Object>> searchCustomMenu(int nuserId) {
		String sql = "SELECT nWxCustomMenuId,nPid,sName,ntype,nKey,nWxUserUrlId,sWxUserUrl,nUserId,nOrder,sRemark FROM tbwxcustommenu where nUserId=" + nuserId + " order by nOrder ";
		return getSimpleJdbcTemplate().queryForList(sql);
	}

	@Override
	public int delSiteText(int nwxTextId) {
		int result = 0;
		// 先获取sRandom 用于删除用户链接地址表
		String sRandom = selectTbWxTextsRandom(nwxTextId);

		if (sRandom != null) {
			String sql = " DELETE FROM tbwxtext where nWxTextId=" + nwxTextId;
			result = getSimpleJdbcTemplate().update(sql);

			if (result > 0) {
				sql = " DELETE FROM tbwxuserurl where sRandom='" + sRandom + "'";
				result = getSimpleJdbcTemplate().update(sql);
			}
		}

		return result > 0 ? 1 : 2;
	}

	public String selectTbWxTextsRandom(int nwxTextId) {
		String sql = "SELECT sRandom FROM tbwxtext where nWxTextId=" + nwxTextId;
		return getSimpleJdbcTemplate().queryForObject(sql, String.class);
	}

	@Override
	public int saveSiteText(TbWxText tbWxText) {
		String sql = "INSERT INTO tbwxtext (sRandom,nUserId,sTitle,sContent,sUrl,nIsValid,dCreateTime) VALUES (:sRandom,:nUserId,:sTitle,:sContent,:sUrl,:nIsValid,:dCreateTime)";
		int result = getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(tbWxText));

		if (result > 0) {
			// 保存用户链接地址表
			TbWxUserUrl tbWxUserUrl = new TbWxUserUrl();
			tbWxUserUrl.setDCreateTime(new Date());
			tbWxUserUrl.setNIsValid(1);
			tbWxUserUrl.setNType(2);
			tbWxUserUrl.setNUserId(tbWxText.getNUserId());
			tbWxUserUrl.setSName(Tools.tpMainText + "--" + tbWxText.getSTitle());
			tbWxUserUrl.setSUrl(tbWxText.getSUrl());
			tbWxUserUrl.setSRandom(tbWxText.getSRandom());
			result = saveTbWxUserUrl(tbWxUserUrl);
		}

		return result > 0 ? 1 : 2;
	}

	public int saveTbWxUserUrl(TbWxUserUrl tbWxUserUrl) {
		String sql = "INSERT INTO tbwxuserurl (nUserId,sName,sUrl,nType,nIsValid,dCreateTime,sRandom) VALUES (:nUserId,:sName,:sUrl,:nType,:nIsValid,:dCreateTime,:sRandom) ";
		int result = getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(tbWxUserUrl));

		return result > 0 ? 1 : 2;
	}

	@Override
	public Page searchSiteText(PageRequest<Map> pageRequest, int userid, int ntype) {
		String sql = "";
		String order = " order by nWxTextId desc ";
		sql += " AND nUserId=" + userid + " AND nIsValid=" + ntype + "  ";

		String sql_count = "SELECT count(1) FROM tbwxtext with (nolock) WHERE 1=1 " + sql;
		String sql_page = "SELECT nWxTextId,sTitle,sContent,nIsValid,dCreateTime FROM tbwxtext with (nolock) WHERE 1=1 " + sql + order;
		return pageQuery(sql_page, sql_count, pageRequest, new BeanPropertyRowMapper(TbWxText.class));
	}

	@Override
	public int updateSiteText(TbWxText tbWxText) {
		String sql = " UPDATE tbwxtext set sTitle = :sTitle,sContent = :sContent,nIsValid=:nIsValid  WHERE nWxTextId=:nWxTextId ";
		int result = getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(tbWxText));

		// 也修改用户链接地址的是否有效
		if (result > 0) {
			String sRandom = selectTbWxTextsRandom(tbWxText.getNWxTextId());
			sql = " UPDATE tbwxuserurl set nIsValid=" + tbWxText.getNIsValid() + "  WHERE sRandom='" + sRandom + "' ";
			result = getSimpleJdbcTemplate().update(sql);
		}
		return result > 0 ? 1 : 2;
	}

	public int delSiteMenu(int nWxMenuId) {
		String sql = " DELETE FROM tbwxmenu where nWxMenuId=" + nWxMenuId;
		int result = getSimpleJdbcTemplate().update(sql);
		return result > 0 ? 1 : 2;
	}

	public int saveSiteMenu(TbWxMenu tbWxMenu) {
		// 获取链接地址
		tbWxMenu.setSWxUserUrl(selectWxUserUrl(tbWxMenu.getNWxUserUrlId()));

		String sql = "INSERT INTO tbwxmenu (nUserId,sContent,sPicUrl,nWxUserUrlId,sWxUserUrl,ntype,nIsValid,dCreateTime,nOrder) "
				+ "VALUES (:nUserId,:sContent,:sPicUrl,:nWxUserUrlId,:sWxUserUrl,:ntype,:nIsValid,:dCreateTime,:nOrder)";
		int result = getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(tbWxMenu));

		return result > 0 ? 1 : 2;
	}

	public Page searchSiteMenu(PageRequest<Map> pageRequest, int userid, int ntype) {
		String sql = " AND nUserId=" + userid + " ";
		String order = "  order by nOrder ";

		if(ntype!=0){
			sql += " AND ntype=" + ntype + "  ";
		}
		

		String sql_count = "SELECT count(1) FROM tbwxmenu with (nolock) WHERE 1=1 " + sql;
		String sql_page = "SELECT nWxMenuId,nUserId,sContent,sPicUrl,nWxUserUrlId,nIsValid,dCreateTime,nOrder,ntype FROM tbwxmenu with (nolock) WHERE 1=1 " + sql + order;
		return pageQuery(sql_page, sql_count, pageRequest, new BeanPropertyRowMapper(TbWxMenu.class));
	}

	public String selectWxUserUrl(int wxUserUrlId) {
		if (wxUserUrlId == 0) {
			return "";
		}
		String sql = "SELECT sUrl FROM tbwxuserurl where nWxUserUrlId=" + wxUserUrlId;
		return getSimpleJdbcTemplate().queryForObject(sql, String.class);
	}

	public int updateSiteMenu(TbWxMenu tbWxMenu) {
		// 获取链接地址
		tbWxMenu.setSWxUserUrl(selectWxUserUrl(tbWxMenu.getNWxUserUrlId()));

		String sql = " UPDATE tbwxmenu set ";
		if (tbWxMenu.getSPicUrl().equals("1")) {
			sql += " sContent = :sContent,nWxUserUrlId = :nWxUserUrlId,sWxUserUrl=:sWxUserUrl,nIsValid = :nIsValid,nOrder = :nOrder,ntype=:ntype ";
		} else {
			sql += " sContent = :sContent,sPicUrl = :sPicUrl,nWxUserUrlId = :nWxUserUrlId,sWxUserUrl=:sWxUserUrl,nIsValid = :nIsValid,nOrder = :nOrder,ntype=:ntype ";
		}
		sql += " WHERE nWxMenuId=:nWxMenuId ";
		int result = getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(tbWxMenu));

		return result > 0 ? 1 : 2;
	}

}
